/*
 * PlayerWorker.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.texttovoice;

import java.io.IOException;
import java.util.regex.Pattern;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class PlayerWorker {
	private final static String TAG = "learningwords";

	public static final int LANG_DE = 1;
	public static final int LANG_EN = 2;

	public static final int PLAY_CANCEL = 0;
	public static final int PLAY_TEXTTOVOICE = 1;
	public static final int PLAY_AUDIO_URI = 2;

	private HandlerThread audioThread;
	AudioMessageHandler handler;
	PlayerListenerI errorListener;
	Pattern bracketsOne = Pattern.compile( "\\{.*\\}" );
	Pattern bracketsTwo = Pattern.compile( "\\[.*\\]" );

	private Context context;

	public PlayerWorker(Context context, PlayerListenerI errorListener) {
		this.context = context;
		this.errorListener = errorListener;
	}
	
	public void setContextAndListener( Context context, PlayerListenerI errorListener) {
		this.context = context;
		this.errorListener = errorListener;
	}

	public void startThread() {

		if (audioThread == null) {
			audioThread = new HandlerThread("Audio Thread");
			audioThread.start();
			Looper looper = audioThread.getLooper();
			handler = new AudioMessageHandler(looper);
		}
	}

	public void stopThread() {
		if (audioThread != null)
			audioThread.interrupt();
	}

	public class AudioMessageHandler extends Handler {
		private MediaPlayer mp;

		public AudioMessageHandler(Looper looper) {
			super(looper);
		}

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == PLAY_TEXTTOVOICE) {
				int language = msg.arg1;
				String text = (String) msg.obj;
				executePlayText(context, language, text);
			} else if (msg.what == PLAY_AUDIO_URI) {
				String uri = (String) msg.obj;
				executePlayUri(context, Uri.parse(uri));
			} else if (msg.what == PLAY_CANCEL) {
				mp.reset();
			}

		}

		public void executePlayText(Context context, int language, String text) {
			String langString;
			if (language == LANG_DE)
				langString = "de";
			else
				langString = "en";

			text = text.replace("Ä", "Ae").replace("ä", "ae")
					.replace("Ö", "Oe").replace("ö", "oe").replace("Ü", "Ue")
					.replace("ü", "ue");
			text = bracketsOne.matcher(text).replaceAll("");
			text = bracketsTwo.matcher(text).replaceAll("");
			
			Uri uri = Uri.parse("http://translate.google.com/translate_tts?tl="
					+ langString + "&q=" + Uri.encode(text));
			executePlayUri(context, uri);
		}

		public void executePlayUri(Context context, Uri uri) {
			Log.d(TAG, "playUri");
			mp = new MediaPlayer();
			try {
				mp.setDataSource(context, uri);
				mp.prepare();
				mp.start();
			} catch (IllegalArgumentException e) {
				if (errorListener != null)
					errorListener.notifyError(e.getMessage());
			} catch (SecurityException e) {
				if (errorListener != null)
					errorListener.notifyError(e.getMessage());
			} catch (IllegalStateException e) {
				if (errorListener != null)
					errorListener.notifyError(e.getMessage());
			} catch (IOException e) {
				if (errorListener != null)
					errorListener.notifyError(e.getMessage());
			}
		}
	}

	public void playText(int language, String text) {
		Message msg = handler.obtainMessage();
		msg.what = PLAY_TEXTTOVOICE;
		msg.arg1 = language;
		msg.obj = text;
		handler.sendMessage(msg);
	}

	public void playUri(String uri) {
		Message msg = handler.obtainMessage();
		msg.what = PLAY_AUDIO_URI;
		msg.obj = uri;
		handler.sendMessage(msg);
	}

	public void playCancel() {
		Message msg = handler.obtainMessage();
		msg.what = PLAY_CANCEL;
		handler.sendMessage(msg);
	}
}
